import {
  deleteContent,down,recall,submit3C,selectReason
} from './commissionsListServ'
// import { parse } from 'qs'
import { message} from 'antd'
// import moment from 'moment'
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// import { hashHistory } from 'dva/router'
// import { getProduct } from './commissionsListServ.js'
// import { makeAddress,download } from "utils/util"
import config from 'config/config'
import $ from 'jquery';

const defultState = {
    biz:{
      queryForm: {}
    },
    submitLoading: false,
    submitFeikongLoading: false,
    backLoading: false,
    loading: false,
    products:[],
    id:"",
    downLoading:false
   
}

const tmpModule = {
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({type: 'updateStore', payload: cloneDeep(defultState)})
        }
      })
    },
  },


  effects: {
    *deleteContent({payload},{put,select,call}){
      
    //   if(yield call(verifyCommissionTokenFun,{})){
        let {id} = yield select(state => state[tmpModule.namespace])
        let params = {
          id: id
        }
        const { success, msg, data } = yield call(deleteContent, {...params})
        let defultState = yield select(state => state[tmpModule.namespace])
        if(success){
          message.success('删除成功')
          defultState.biz.refreshListDatas(true)
        }
    //   }
      
    },
    *selectReason({payload},{put,select,call}){
    //   if(yield call(verifyCommissionTokenFun,{})){
        let {id} = yield select(state => state[tmpModule.namespace])
        let params = {
          id: id
        }
        const { success, msg, data } = yield call(selectReason, {...params})
        if(success){
          message.success('查询成功')
        }
    //   }
      
    },
    *down({payload}, {put, call, select}) {
        
        // if(yield call(verifyCommissionTokenFun,{})){
            yield put({type: 'updateStore', payload: {downLoading: true,loading:true}})
          let {biz,id} = yield select(state => state[tmpModule.namespace])
          let params = {
            id: id,
            ...biz.queryForm 
          }
          const {result, success} = yield call(down,params)
          if (success) {
            var $a = $("<a>");
            $a.attr("href", result.url);
            $a.attr("download", '佣金申请单明细');
            $("body").append($a);
            $a[0].click();
            $a.remove();
            message.success('导出成功,正在为您下载表格')
            yield put({type: 'updateStore', payload: {downLoading: false,loading:false}})
          }
        // }
        
    },

    *recall({payload},{put,select,call}){
    //   if(yield call(verifyCommissionTokenFun,{})){
        let {id} = yield select(state => state[tmpModule.namespace])
        yield put({type: 'updateStore', payload: {backLoading: true,loading:true}})
        let params = {
          hid: id
        }
        const { success, code, data } = yield call(recall, {...params})
        let defultState = yield select(state => state[tmpModule.namespace])
        if(code){
          yield put({type: 'updateStore', payload: {backLoading: false,loading:false}})
        }
        if(success){
          message.success('撤回成功')
          defultState.biz.refreshListDatas(true)
        }
    //   }
      
    },
    *submit3C({payload},{put,select,call}){
    //   if(yield call(verifyCommissionTokenFun,{})){
        let {id} = yield select(state => state[tmpModule.namespace])
        yield put({type: 'updateStore', payload: {submitLoading: true,loading:true}})
        let params = {
          hid: id
        }
        const { success, code, data } = yield call(submit3C, {...params})
        let defultState = yield select(state => state[tmpModule.namespace])
        if(code){
          yield put({type: 'updateStore', payload: {submitLoading: false,loading:false}})
        }
        if(success){
          message.success('提交3C流程成功')
          defultState.biz.refreshListDatas(true)
        }
    //   }
    },
    
  },
  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
